package com.graduation.springbootinit.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.graduation.springbootinit.common.ErrorCode;
import com.graduation.springbootinit.exception.BusinessException;
import com.graduation.springbootinit.mapper.OutfitWorkMapper;
import com.graduation.springbootinit.mapper.OutfitWorkLikeMapper;
import com.graduation.springbootinit.model.entity.ActivityPart.OutfitWork;
import com.graduation.springbootinit.model.entity.ActivityPart.OutfitWorkLike;
import com.graduation.springbootinit.service.OutfitWorkService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class OutfitWorkServiceImpl extends ServiceImpl<OutfitWorkMapper, OutfitWork>
        implements OutfitWorkService {

    @Resource
    private OutfitWorkLikeMapper outfitWorkLikeMapper;

    @Override
    public void submitWork(OutfitWork work) {
        // 验证参数
        if (work.getChallengeId() == null || work.getTitle() == null || work.getContent() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR, "参数不完整");
        }
        // 设置初始值
        work.setLikeCount(0);
        save(work);
    }

    @Override
    public Page<OutfitWork> getWorksByChallenge(Long challengeId, int pageNum, int pageSize) {
        Page<OutfitWork> page = new Page<>(pageNum, pageSize);
        return baseMapper.selectWorksByChallenge(page, challengeId);
    }

    @Override
    public List<OutfitWork> getWorksByUser(Long userId) {
        return baseMapper.selectWorksByUser(userId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void likeWork(Long workId, Long userId) {
        // 检查是否已点赞
        if (hasUserLiked(workId, userId)) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "已经点赞过了");
        }
        
        // 创建点赞记录
        OutfitWorkLike like = new OutfitWorkLike();
        like.setWorkId(workId);
        like.setUserId(userId);
        outfitWorkLikeMapper.insert(like);
        
        // 增加作品点赞数
        baseMapper.incrementLikeCount(workId);
    }

    @Override
    public List<OutfitWork> getTopThreeWorks(Long challengeId) {
        return baseMapper.selectTopThreeWorks(challengeId);
    }

    @Override
    public boolean hasUserLiked(Long workId, Long userId) {
        return outfitWorkLikeMapper.checkUserLiked(workId, userId) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void unlikeWork(Long workId, Long userId) {
        // 检查是否已点赞
        if (!hasUserLiked(workId, userId)) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR, "还没有点赞");
        }
        
        // 删除点赞记录
        outfitWorkLikeMapper.delete(new QueryWrapper<OutfitWorkLike>()
                .eq("work_id", workId)
                .eq("user_id", userId));
        
        // 减少作品点赞数
        baseMapper.decrementLikeCount(workId);
    }

    @Override
    public OutfitWork getWorkById(Long workId) {
        OutfitWork work = getById(workId);
        if (work == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR, "作品不存在");
        }
        return work;
    }
} 